package cn.wolfcode.luowowo.redis.service.impl;

import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.redis.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.redis.util.DateUtil;
import cn.wolfcode.luowowo.redis.util.RedisKeys;
import cn.wolfcode.luowowo.redis.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.service.IStrategyService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
public class StrategyStatisVORedisServiceImpl implements IStrategyStatisVORedisService{
    @Autowired
    private StringRedisTemplate template;
    @Autowired
    private IStrategyService strategyService;

    @Override
    public void viewnumIncrease(String sid, int num) {
        StrategyStatisVO vo = getStrategyStatisVo(sid);
        vo.setViewnum(vo.getViewnum()+num);
        setStrategyStatisVo(vo);
    }

    @Override
    public StrategyStatisVO getStrategyStatisVo(String sid) {
        //判断vo对象是否存在
        //先获取该对象的key
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        StrategyStatisVO vo = null;
        if(!template.hasKey(key)){
            //如果不存在Key,创建新的vo对象
            vo = new StrategyStatisVO();
            Strategy strategy = strategyService.get(sid);
            BeanUtils.copyProperties(strategy,vo);
            vo.setStrategyId(sid);
            template.opsForValue().set(key,JSON.toJSONString(vo));
        }else{
            //如果存在，直接返回
            String strVo = template.opsForValue().get(key);
            vo = JSON.parseObject(strVo,StrategyStatisVO.class);

        }
        return vo;
    }

    @Override
    public void setStrategyStatisVo(StrategyStatisVO vo) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        template.opsForValue().set(key,JSON.toJSONString(vo));
    }

    @Override
    public void replynumIreplynum(String strategyId, int i) {
        StrategyStatisVO vo = getStrategyStatisVo(strategyId);
        vo.setReplynum(vo.getReplynum()+i);
        setStrategyStatisVo(vo);
    }

    @Override
    public boolean favor(String sid, String uid) {
        //根据uid去创建key,根据Key去查询Value看看有没传入的Sid；
        String key = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        StrategyStatisVO vo = getStrategyStatisVo(sid);
        List<String> list = null;
       //判断key是否存在
        if(!template.hasKey(key)){
            //如果不存在这创建一个集合，把Sid存入集合里，收藏数加1
            list = new ArrayList<>();
            vo.setFavornum(vo.getFavornum()+1);
            list.add(sid);
           template.opsForValue().set(key,JSON.toJSONString(list));
           setStrategyStatisVo(vo);
           return true;
        }else{
            //如果存在，則拿出value,在將value的Sid拿出來和传进来得到对比
            String str = template.opsForValue().get(key);
            list = JSON.parseArray(str,String.class);
        }
        //判断sid是否存在于list中
        if(list.contains(sid)){
            //存在，则把sid移除并减一
            vo.setFavornum(vo.getFavornum()-1);
            list.remove(sid);
            template.opsForValue().set(key,JSON.toJSONString(list));
            setStrategyStatisVo(vo);
            return false;
        }else{
            vo.setFavornum(vo.getFavornum()+1);
            list.add(sid);
            template.opsForValue().set(key,JSON.toJSONString(list));
            setStrategyStatisVo(vo);
            return true;
        }
    }

    @Override
    public List<String> getSids(String uid) {
        String listKey = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        List<String> list = null;  //攻略id收藏集合list
        if(template.hasKey(listKey)){ //如果已经存在了，获取
            String listStr = template.opsForValue().get(listKey);
            //将字符串转换成list集合， 参数2：集合泛型
            list = JSON.parseArray(listStr, String.class);
        }else{
            //最初，攻略id收藏集合list根本不存在， 需要创建并初始化
            list = new ArrayList<>();
        }
        return list;
    }

    @Override
    public boolean strategyThumbup(String sid, String uid) {
        String key = RedisKeys.USER_STRATEGY_FAVOR.join(uid,sid);
        if(template.hasKey(key)){
            //存在则说明顶过了，返回false;
            return false;
        }else{
            //不存在的话，数据加一，重置然后重置定的时间，在设置回去redis
            StrategyStatisVO vo = getStrategyStatisVo(sid);
            vo.setThumbsupnum(vo.getThumbsupnum()+1);
            setStrategyStatisVo(vo);
            Date now = new Date();
            //今天最后一秒那个时间- now 到秒数
            Date endDate = DateUtil.getEndDate(now);  //今天最后一秒时间对象
            long time = DateUtil.getDateBetween(now, endDate);  //有效时间， 单位秒
            template.opsForValue().set(key, "1",time, TimeUnit.SECONDS);
            return true;
        }
    }

    @Override
    public boolean isVoExists(String id) {
        String key = RedisKeys.USER_STRATEGY_FAVOR.join(id);
        return template.hasKey(key);
    }
}
